package com.yeook.ymax.ui.activitys;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.yeook.ymax.db.Database;
import com.yeook.ymax.ui.activitys.HandlerWallaper.ElementHandler;
import com.yeook.ymax.ui.entity.RingtoneView;

public class HandlerRingtone  extends DefaultHandler{
	Map<String, ElementHandler> elements = new HashMap<String, ElementHandler>();
	protected Database db = Database.getInstance();
	StringBuilder sb  = new StringBuilder();
	
	private RingtoneView ringtoneView = null;
	
	interface ElementHandler {
		void startElement(Attributes atts) throws SAXException;
		void endElement() throws SAXException;
	}
	
	public HandlerRingtone()
	{
		loadSpecificElements();
	}
	
	void loadSpecificElements() {
		elements.put("ringtone", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {
				ringtoneView = new RingtoneView();
			}

			@Override
			public void endElement() throws SAXException {
				db.insertRingtoneView(ringtoneView);
			}
		});
		
		elements.put("dwn", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {

			}

			@Override
			public void endElement() throws SAXException {
				String dwn = sb.toString() == null || sb.toString().equals("null") ? "0" : sb.toString();
				ringtoneView.setDwn(Integer.valueOf(dwn));
			}
		});
		
		elements.put("description", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {

			}

			@Override
			public void endElement() throws SAXException {
				String description = sb.toString() == null || sb.toString().equals("null") ? "" : sb.toString();
				ringtoneView.setDescription(description);
			}
		});
		
		elements.put("date", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {

			}

			@Override
			public void endElement() throws SAXException {
				String date = sb.toString() == null || sb.toString().equals("null") ? "" : sb.toString();
				SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.FFFFFFF" );
				try {
					if("".equals(date))
					{
						ringtoneView.setDate(new Date());
					}
					else{
						ringtoneView.setDate(formatDate.parse(date));
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		});
		
		elements.put("id", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {

			}

			@Override
			public void endElement() throws SAXException {
				String id = sb.toString() == null || sb.toString().equals("null") ? "0" : sb.toString();
				ringtoneView.setUniqueId(Long.valueOf(id));
			}
		});
		
		elements.put("tags", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {

			}

			@Override
			public void endElement() throws SAXException {
				String tags = sb.toString() == null || sb.toString().equals("null") ? "" : sb.toString();
				ringtoneView.setTags(tags);
			}
		});
		
		elements.put("lable", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {

			}

			@Override
			public void endElement() throws SAXException {
				String lable = sb.toString() == null || sb.toString().equals("null") ? "" : sb.toString();
				ringtoneView.setLable(lable);
			}
		});
		
		elements.put("fileName", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {

			}

			@Override
			public void endElement() throws SAXException {
				String fileName = sb.toString() == null || sb.toString().equals("null") ? "" : sb.toString();
				ringtoneView.setFileName(fileName);
			}
		});
		
		elements.put("cateCode", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {

			}

			@Override
			public void endElement() throws SAXException {
				String cateCode = sb.toString() == null || sb.toString().equals("null") ? "0" : sb.toString();
				ringtoneView.setCateCode(Integer.valueOf(cateCode));
			}
		});
		
		elements.put("hash", new ElementHandler() {

			@Override
			public void startElement(Attributes atts) throws SAXException {

			}

			@Override
			public void endElement() throws SAXException {
				db.insertHash(sb.toString(), "ringtone");

			}
		});
	}
	
	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void startElement(String uri, String localName,
			String qName, Attributes attributes)
			throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		sb.setLength(0);
		ElementHandler elementHandler = elements.get(localName);

		if (elementHandler != null) {
			elementHandler.startElement(attributes);
		} else {
//			System.out.println("Element not found:" + localName);
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		sb.append(ch,start,length);
	}

	@Override
	public void endElement(String uri, String localName,
			String qName) throws SAXException {
		super.endElement(uri, localName, qName);

		ElementHandler elementHandler = elements.get(localName);

		if (elementHandler != null) {
			elementHandler.endElement();
		} else {
		}
	}
}
